#include "maintask.h"

MainTask::MainTask(QObject *parent) : QObject(parent) {
  tcpServer = new NetworkServer();
  powerMeter = new PowerMeter();
  powerMeterTimer = new QTimer();
  blinkTimer = new QTimer();
  extControl = new ExtControl();
  i2c2 = new I2C(1);  //Create I2C1 control bus


  QSettings appSettings(QDir::currentPath() + "/settings.ini",QSettings::IniFormat,0);

  //Load settings from the settings.ini file
  loadSettings(appSettings);

  //Connect the network server
  connect(tcpServer,SIGNAL(dataReady(QByteArray)),this,SLOT(networkDataReady(QByteArray)));
  connect(tcpServer, SIGNAL(socketError(int,QString)), this, SLOT(networkError(int, QString)));
  connect(powerMeter, SIGNAL(valueChanged()), this, SLOT(powerMeterSendData()));
  connect(tcpServer, SIGNAL(clientConnected()), this, SLOT(networkClientConnected()));
  connect(tcpServer, SIGNAL(clientDisconnected()), this, SLOT(networkClientDisconnected()));

  connect(blinkTimer, SIGNAL(timeout()), extControl, SLOT(blinkTimerTimeout()));
  connect(powerMeterTimer, SIGNAL(timeout()), powerMeter, SLOT(powerMeterEvent()));

  extControl->initGPIO();

  //Turn on all power to the electronics
  extControl->setExtPowerOn();
  QTimer::singleShot(STARTUP_DELAY, this, SLOT(init()));
}

MainTask::~MainTask() {
  extControl->setExtPowerOff();

  qDebug() << "Power OFF";
}

void MainTask::loadSettings(QSettings &settings) {
  settings.beginGroup("Network");
  tcpServer->setHostIP(settings.value("ServerIP").toString());
  tcpServer->setTcpPort(settings.value("TcpPort").toInt());
  tcpServer->setClientIP(settings.value("ClientIP").toString());
  settings.endGroup();

  settings.beginGroup("PowerMeter");

  powerMeter->setPickupType(settings.value("PickupType").toInt());
  powerMeter->setAlarmLevelVSWR(settings.value("AlarmLevelVSWR").toDouble());
  powerMeter->setAlarmLevelFWDPower(settings.value("AlarmLevelFWDPower").toInt());
  powerMeter->setAlarmLevelREFPower(settings.value("AlarmLevelREFPower").toInt());
  powerMeter->setDataPollInterval(settings.value("DataPollInterval").toInt());

  settings.endGroup();
}

void MainTask::init() {
  //Start the TCP Server and wait for a connection
  tcpServer->startServer();

  powerMeterTimer->setInterval(powerMeter->getDataPollInterval());
  powerMeterTimer->start();

  blinkTimer->setInterval(200);
  blinkTimer->start();

  //Enable external I2C communication
  extControl->setExtI2COn();

  if (i2c2->writeI2CDevice(0x26,3,0) == 0)
    qDebug() << "SUCCESS";

  if (i2c2->writeI2CDevice(0x26,1,85) == 0)
    qDebug() << "SUCCESS";

  qint8 data = 0;
  if (i2c2->readI2CDevice(0x26,0,&data) == 0) {
    qDebug() << "SUCCESS: " << data;
  }
}

void MainTask::networkDataReady(QByteArray data) {
  qDebug() << "MainTask: Network data RXed";
}

void MainTask::powerMeterSendData() {
  if (tcpServer->isClientConnected()) {
    QJsonDocument jsonDocument;
    QJsonObject jsonObj;
    jsonObj["NetworkDataType"] = NETWORK_DATA_TYPE_POWERMETER;

    powerMeter->getData(jsonObj);
    jsonDocument.setObject(jsonObj);
    tcpServer->sendData(jsonDocument.toBinaryData());
  }
}

void MainTask::networkError(const int &errorCode, const QString &errorString) {
  qDebug() << "Server: ERROR[" << errorCode << "] >>" << errorString;
}

void MainTask::networkClientConnected() {
  extControl->setLED2On();
  extControl->setExtPowerOn();
}

void MainTask::networkClientDisconnected() {
  extControl->setLED2Off();
  extControl->setExtPowerOff();
}
